package crack.ch1;

/**
 * @author Pavel Belevich
 *         Date: 6/16/12
 *         Time: 11:01 PM
 */
public class Problem3 {

    static String sort(String s) {
        char[] content = s.toCharArray();
        java.util.Arrays.sort(content);
        return new String(content);
    }
    
    static boolean permutation(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        return sort(s).equals(sort(t));
    }
    
    static boolean permutation2(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        
        int[] letters = new int[256];
        
        char[] s_array = s.toCharArray();
        for (char c : s_array) {
            letters[c]++;
        }
        
        for (int i = 0 ; i < t.length() ; i++) {
            int c = t.charAt(i);
            if (--letters[c] < 0) {
                return false;
            }
        }

        return true;
    }
    
    public static void main(String[] args) {
        System.out.println(permutation("madonnalouiseciccone", "onecooldancemusician"));
        System.out.println(permutation2("madonnalouiseciccone", "onecooldancemusician"));
    }
    
}
